package com.nexgo.payment.databases.mapper;

import com.nexgo.payment.constrant.TransType;
import com.nexgo.payment.databases.Classification;
import com.nexgo.payment.databases.SettleInfo;
import com.nexgo.payment.databases.Statistics;
import com.nexgo.payment.databases.TransRecord;

import java.util.List;

/**
 * Created by xiaox on 2017/9/4.
 */

/**
 * 结算信息转化
 */
public class SettleMapper {
    /**
     * 交易记录转换为结算信息
     * @param transRecords 成功的交易记录
     * @return 结算信息
     */
    public SettleInfo transform(List<TransRecord> transRecords) {
        SettleInfo settleInfo = new SettleInfo();
        Statistics domesticStatistics = new Statistics();
        domesticStatistics.setId(0L);
        Statistics internationalStatistics = new Statistics();
        internationalStatistics.setId(0L);
        Statistics scanStatistics = new Statistics();
        scanStatistics.setId(0L);
        Classification domesticClassification = new Classification();
        domesticClassification.setId(0L);
        Classification internationalClassification = new Classification();
        internationalClassification.setId(0L);
        Classification scanClassification = new Classification();
        scanClassification.setId(0L);

        settleInfo.setStatistics(domesticStatistics);
        settleInfo.setInterStatistics(internationalStatistics);
        settleInfo.setClassification(domesticClassification);
        settleInfo.setInterClassification(internationalClassification);
        //lr
        settleInfo.setScanStatistics(scanStatistics);
        settleInfo.setScanClassification(scanClassification);
        //end
        Statistics statistics;
        Classification classification;
        for (TransRecord transRecord : transRecords) {
            //跳过不成功的
            if (!transRecord.getIsSuccess()) {
                continue;
            }
            if(transRecord.getPayTraceCode() == null) {
                if (transRecord.getIsInternational()) {
                    //外卡
                    statistics = internationalStatistics;
                    classification = internationalClassification;
                } else {
                    //内卡
                    statistics = domesticStatistics;
                    classification = domesticClassification;
                }
            }else{
                //扫码
                statistics = scanStatistics;
                classification = scanClassification;
            }
            switch (TransType.values()[transRecord.getTransType()]) {
                case SALE:
                case OFF_SALE:
                case SCAN_SALE:
                case SCAN_ORDER://lr add
                    classification.setSaleNum(classification.getSaleNum() + 1);
                    classification.setSaleAmt(classification.getSaleAmt() + Long.parseLong(transRecord.getAmount()));
                    statistics.setDebitNum(statistics.getDebitNum() + 1);
                    statistics.setDebitAmt(statistics.getDebitAmt() + Long.parseLong(transRecord.getAmount()));
                    break;
                case EC_SALE:
                    classification.setEcSaleNum(classification.getEcSaleNum() + 1);
                    classification.setEcSaleAmt(classification.getEcSaleAmt() + Long.parseLong(transRecord.getAmount()));
                    statistics.setDebitNum(statistics.getDebitNum() + 1);
                    statistics.setDebitAmt(statistics.getDebitAmt() + Long.parseLong(transRecord.getAmount()));
                    break;
                case AUTH_CM:
                    classification.setAuthCMNum(classification.getAuthCMNum() + 1);
                    classification.setAuthCMAmt(classification.getAuthCMAmt() + Long.parseLong(transRecord.getAmount()));
                    statistics.setDebitNum(statistics.getDebitNum() + 1);
                    statistics.setDebitAmt(statistics.getDebitAmt() + Long.parseLong(transRecord.getAmount()));
                    break;
                case REFUND:
                case SCAN_REFUND:
                    classification.setRefundNum(classification.getRefundNum() + 1);
                    classification.setRefundAmt(classification.getRefundAmt() + Long.parseLong(transRecord.getAmount()));
                    statistics.setCreditNum(statistics.getCreditNum() + 1);
                    statistics.setCreditAmt(statistics.getCreditAmt() + Long.parseLong(transRecord.getAmount()));
                    break;
                case AUTH_CM_NOTIFY:
                    classification.setAuthCMNotifyNum(classification.getAuthCMNotifyNum() + 1);
                    classification.setAuthCMNotifyAmt(classification.getAuthCMNotifyAmt() + Long.parseLong(transRecord.getAmount()));
                    statistics.setDebitNum(statistics.getDebitNum() + 1);
                    statistics.setDebitAmt(statistics.getDebitAmt() + Long.parseLong(transRecord.getAmount()));
                    break;
                case VOID:
                case SCAN_VOID:
                    classification.setSaleNum(classification.getSaleNum() - 1);
                    classification.setSaleAmt(classification.getSaleAmt() - Long.parseLong(transRecord.getAmount()));
                    classification.setVoidNum(classification.getVoidNum() + 1);
                    classification.setVoidAmt(classification.getVoidAmt() + Long.parseLong(transRecord.getAmount()));
                    statistics.setCreditNum(statistics.getCreditNum() + 1);
                    statistics.setCreditAmt(statistics.getCreditAmt() + Long.parseLong(transRecord.getAmount()));
                    break;
                case AUTH_CM_VOID:
                    classification.setAuthCMNum(classification.getAuthCMNum() - 1);
                    classification.setAuthCMAmt(classification.getAuthCMAmt() - Long.parseLong(transRecord.getAmount()));
                    classification.setAuthCMVoidNum(classification.getAuthCMVoidNum() + 1);
                    classification.setAuthCMVoidAmt(classification.getAuthCMVoidAmt() + Long.parseLong(transRecord.getAmount()));
                    statistics.setCreditNum(statistics.getCreditNum() + 1);
                    statistics.setCreditAmt(statistics.getCreditAmt() + Long.parseLong(transRecord.getAmount()));
                    break;
                default:
                    break;
            }
        }
        return settleInfo;
    }
}
